// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.property.NodeParameterUtils;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.cmd.PropertyChangeTextCommand;
import org.dengues.ui.web.URLDialog;
import org.eclipse.core.runtime.Path;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class WebPropertyController extends AbstractPropertyController {

    private Text tdText;

    private Text urlText;

    /**
     * Qiang.Zhang.Adolf@gmail.com WebPropertyController constructor comment.
     * 
     * @param dps
     */
    public WebPropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(final Composite composite, final NodePropertyParameter parameter, final Control lastControl) {
        final Composite subcomposite = new Composite(composite, SWT.NONE);
        GridLayout gridLayout = new GridLayout(5, false);
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;
        subcomposite.setLayout(gridLayout);
        subcomposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        subcomposite.setBackground(composite.getBackground());
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, parameter.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        Text text = getWidgetFactory().createText(subcomposite, "", SWT.BORDER); //$NON-NLS-1$
        ControlDecoration controlDecoration = new ControlDecoration(text, SWT.RIGHT | SWT.TOP);
        if (parameter.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        urlText = (Text) controlDecoration.getControl();

        register(parameter, urlText);
        String[] values = new String[0];
        if (parameter.getValue() != null) {
            values = NodeParameterUtils.delimitedParameterValue(parameter.getValue().toString(), false);
            if (values.length > 0) {
                urlText.setText(values[0]);
            }
        }
        cLabel = getWidgetFactory().createCLabel(subcomposite, "TD"); //$NON-NLS-1$
        gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL / 2;
        cLabel.setLayoutData(gridData);

        text = getWidgetFactory().createText(subcomposite, "", SWT.BORDER); //$NON-NLS-1$
        controlDecoration = new ControlDecoration(text, SWT.RIGHT | SWT.TOP);
        if (parameter.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        tdText = (Text) controlDecoration.getControl();

        register(parameter, tdText);

        if (values.length > 1) {
            tdText.setText(values[1]);
        }

        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.heightHint = SWT.DEFAULT;
        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        urlText.setLayoutData(data);
        urlText.setData(PARAMETER_NAME, parameter.getName());
        urlText.setEditable(!parameter.isReadOnly());
        Button browse = getWidgetFactory().createButton(subcomposite,
                Messages.getString("FilePropertyController.browse"), SWT.PUSH); //$NON-NLS-1$
        browse.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                String open = null;
                String td = null;
                Path path = null;
                URLDialog dialog = new URLDialog(composite.getShell(), urlText.getText(), Messages.getString(
                        "FilePropertyController.title", ((CompNode) dps //$NON-NLS-1$
                                .getElem()).getUniqueName(), parameter.getDisplayName()));
                if (dialog.open() == URLDialog.OK) {
                    open = dialog.getUrlText();
                    td = dialog.getUrlTD();
                }
                if (open != null) {
                    path = new Path(open);
                    urlText.setText(path.toPortableString());
                    tdText.setText(td);
                    subcomposite.layout();
                    PropertyChangeTextCommand command = new PropertyChangeTextCommand(dps.getElem(), parameter.getName(), urlText
                            .getText()
                            + PARAMETER_SEPERATE + tdText.getText().replaceAll("\n", "")); //$NON-NLS-1$ //$NON-NLS-2$
                    getCommandStack().execute(command);
                }
            }
        });
        curControlMap.put(parameter.getName(), urlText);
        return browse;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#register(org.dengues.core.components.NodePropertyParameter,
     * org.eclipse.swt.widgets.Control)
     */
    @Override
    protected void register(final NodePropertyParameter param, final Control labelText) {
        labelText.addKeyListener(new KeyAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
             */
            @Override
            public void keyReleased(KeyEvent e) {
                Command command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), urlText.getText()
                        + PARAMETER_SEPERATE + tdText.getText());
                getCommandStack().execute(command);
            }

        });
    }
}
